package qualomelhor.gwt.modules.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.search.client.ExpandMode;
import com.google.gwt.search.client.ImageSearch;
import com.google.gwt.search.client.Result;
import com.google.gwt.search.client.*;
import com.google.gwt.search.client.ResultSetSize;
import com.google.gwt.search.client.SearchControl;
import com.google.gwt.search.client.SearchControlOptions;
import com.google.gwt.search.client.SearchResultsHandler;
import com.google.gwt.search.client.WebResult;
import com.google.gwt.search.client.WebSearch;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SimpleSearch implements EntryPoint {

	private RootPanel root;
	int currentRow;
	FlexTable resultsTable;

	@Override
	public void onModuleLoad() {
		System.out.println("Iniciando modulo...");
		root = RootPanel.get("divCadastro");
		root.clear();
		root.add(new Label("Busca:"));
		buscaViaWidget();
	}

	private void buscaViaWidget() {
		final TextBox t = new TextBox();
		Button b = new Button("procura");
		final BuscaImagemWidget bi = new BuscaImagemWidget();
		b.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				bi.setBusca(t.getText());
			}
		});

		root.add(t);
		root.add(b);
		root.add(bi);
	}
	
	private void buscaSimples() {
	    SearchControlOptions options = new SearchControlOptions();
	    WebSearch webSearch = new WebSearch();
	    webSearch.setResultSetSize(ResultSetSize.LARGE);
	    ImageSearch imageSearch = new ImageSearch();
	    options.add(imageSearch, ExpandMode.CLOSED);
	    final SearchControl control = new SearchControl(options);
	    control.execute("treehouse");

	    root.add(control);
	}

	private void buscaComTratamento() {
		resultsTable = new FlexTable();
	    SearchControlOptions options = new SearchControlOptions();

	    ImageSearch webSearch = new ImageSearch();
	    // Choose no HTML generation for quicker results.
	    //webSearch.setNoHtmlGeneration();
	    options.add(webSearch, ExpandMode.PARTIAL);
	    final SearchControl control = new SearchControl(options);
	    control.addSearchResultsHandler(new SearchResultsHandler() {
	    	public void onSearchResults(SearchResultsEvent event) {
	    		JsArray<? extends Result> results = event.getResults();

	    		System.out.println("número de resultados: " + results.length());

	    		for (int i = 0; i < results.length(); i++) {
	    			if (results.get(i).getResultClass().equals(ResultClass.IMAGE_SEARCH_RESULT)) {
	    				currentRow++;

	    				WebResult result = (WebResult) results.get(i);
	    				resultsTable.setText(currentRow, 0, "" + currentRow);
	    				resultsTable.setHTML(currentRow, 1, "Link" + i + "<img src=\"" + result.getUrl() + "\" style=\"width:100px; height:100px;\"><BR>");
	    			}
	    		}
	    	}
	    });
	    final TextBox textBox = new TextBox();
	    textBox.setText("treehouse");
	    Button button = new Button("Run Search");
	    button.addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent event) {
	    		for (int i = 0; resultsTable.getRowCount() > 0; ++i) {
	    			resultsTable.removeRow(0);
	    		}
	    		currentRow = 0;
	    		control.execute(textBox.getText());
	    	}
	    });

	    VerticalPanel vp = new VerticalPanel();
	    vp.add(textBox);
	    vp.add(button);
	    vp.add(resultsTable);
	    root.add(vp);
	}	
}